package com.objcat.playground.leetcode;

import java.util.ArrayList;

public class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    static ListNode createNode(ArrayList<Integer> arr) {
        ListNode head = null;
        ListNode lastNode = null;
        for (int i = 0; i < arr.size(); i++) {
            ListNode node = new ListNode();
            node.val = arr.get(i);
            if (lastNode != null) {
                lastNode.next = node;
                lastNode = node;
            } else {
                lastNode = node;
                head = node;
            }
        }
        return head;
    }

    @Override
    public String toString() {
        String result = "[";
        ListNode node = this;
        for (int i = 0; ; i++) {
            if (i == 0) {
                result += node.val;
            } else {
                result += "," + node.val;
            }
            if (node.next == null) {
                break;
            }
            node = node.next;
        }
        result += "]";
        return result;
    }
}

